package com.zzptc.zzsgz.bdsjws.fragments;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.AddActivity;
import com.zzptc.zzsgz.bdsjws.activities.SOSActivity;
import com.zzptc.zzsgz.bdsjws.adapter.SOSAdapter;
import com.zzptc.zzsgz.bdsjws.fragments.base.BaseFragmentbase;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by zzsgz on 2017/5/27.
 */

public class SecondFragment extends BaseFragmentbase {


    @BindView(R.id.et_add)
    EditText etAdd;
    @BindView(R.id.img_add)
    ImageView imgAdd;
    @BindView(R.id.btn_yes)
    Button btnYes;
    @BindView(R.id.btn_succ)
    Button btn_succ;
    Unbinder unbinder;
    @BindView(R.id.rv_sos_tel)
    RecyclerView recyclerView;
    Unbinder unbinderl;
    SOSAdapter sosAdapter;

    @Override
    public void initView() {
        etAdd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                btnYes.setVisibility(View.VISIBLE);
                imgAdd.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    @Override
    public void initData() {

    }

    @Override
    public int getXML() {
        return R.layout.second_fragment;
    }


    @OnClick(R.id.img_add)
    public void onViewClicked() {
        //Toast.makeText(getActivity(), "======", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(getActivity(), AddActivity.class));
        getActivity().findViewById(R.id.tv_count);
    }
      @OnClick(R.id.btn_succ)
      public  void  toThirdFragment(){
          //跳转之前，要获取求救电话（放数据） 电话内容（共享首选项） 定位（服务）
          SharedPreferences shared= MyUtil.getSharedPre();
   //       String sosInfo=shared.getString("sos",null);
          SharedPreferences.Editor editor=shared.edit();
          editor.putString("sos","上课测试，请勿当真。我遇到危险，请来求救我。");
          editor.commit();
          //求救电话放入数据库  并不是增加数据 而是对已有数据库的表增加一列
         for (int i=0;i<MyApplication.getSosList().size();i++){
             Contact c=MyApplication.getSosList().get(i);
           //  if (MyUtil.getAllContacts().contains())
         }

          //跳转

       //   Toast.makeText(getActivity(), "?????", Toast.LENGTH_SHORT).show();
          if (MyApplication.getSosList().size()==0){
              Toast.makeText(getActivity(), "请选择求救联系人", Toast.LENGTH_SHORT).show();
          }else {
              //跳转
              if (getActivity()instanceof SOSActivity){
                  ((SOSActivity) getActivity()).toThirdFragment();
              }
          }
      }


    @OnClick(R.id.btn_yes)
    public void onYesClicked() {
        //先判断电话号码的长度  并且要获取电话号码
        String tel=etAdd.getText().toString().trim();
        if(tel.length()==11&&tel.matches("^1[3578]\\d{9}$")){
            //放入集合中 问题：集合中那创建
            //数据来自两个方面1）记得电话号码 ，直接输入2）从手机联系人中选取
            List<Contact> listSos= MyApplication.getSosList();
            Contact contact=new Contact();
            contact.setPhonenum(tel);
            if (listSos.size()<3) {
                listSos.add(contact);
            }else {
                Toast.makeText(getActivity(),"yingjingyou",Toast.LENGTH_SHORT).show();
            }
            //适配器（重新构造一个  项）   数据源  布局管理
            //首先构建项的布局
            sosAdapter=new SOSAdapter(listSos);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.setAdapter(sosAdapter);

        }
        //判断电话号码的长度
        etAdd.setText("");
        btnYes.setVisibility(View.GONE);
        imgAdd.setVisibility(View.VISIBLE);

    }

    /**
     * 碎片出现时候就会调用
     */
    @Override
    public void onResume() {
        super.onResume();
        if (sosAdapter!=null) {
            sosAdapter.notifyDataSetChanged();
            // Toast.makeText(getActivity(), "=======", Toast.LENGTH_SHORT).show();
        }
    }
}
